/*
 ============================================================================
 Name        : Process.c
 Author      : James McQueen, Corwyn Simpson, Craig Markham
 Version     :
 Date        : 05/14/2012
 Description : TCSS-422 Operating Systems Scheduler
 ============================================================================
 */
#include <stdlib.h>
#include "Process.h"

//Allocate memory for the array of requests, some arbitrary
//multiple of the size of a request.
//Once request array is initialized, all you need to do is
//Place it at the end of the request list.

Process* process_init()
{
	Process* this = (Process*) malloc(sizeof(Process));
	Request** request_ptrptr =
			(Request**)malloc(DEFAULT_REQ_SIZE * sizeof(Request*));
	this->my_length = 0;
	this->my_num_requests = 0;
	this->my_requests_ptr = request_ptrptr;
	this->clock_count = 0;
	this->start_clock = 0;

	return this;
}

void process_destroy(Process* process)
{
    int i;
    for (i = 0; i < process->my_num_requests; i++)
    {
        free(process->my_requests_ptr[i]);
    }
    free(process->my_requests_ptr);
    free(process);
}

int process_getLength(Process* process)
{
	return process->my_length;
}
int process_getNumRequests(Process* process)
{
	return process->my_num_requests;
}
int process_getClock(Process* process)
{
	return process->clock_count;
}
Request** process_getRequests(Process* process)
{
	return process->my_requests_ptr;
}
void process_setNumSteps(Process* process, int length)
{
	process->my_length = length;
}
void process_setNumRequests(Process* process, int num_requests)
{
	process->my_num_requests = num_requests;
}
int process_addRequest(Process* process, Request request)
{
	if(process->my_num_requests == DEFAULT_REQ_SIZE)
	{
		return -1;
	}
	else
	{
		Request* this = (Request*) malloc(sizeof(Request));
		this->enumeration = request.enumeration;
		this->time = request.time;
		this->type = request.type;

		process->my_requests_ptr[process->my_num_requests] = this;
		process->my_num_requests++;

		return 0;
	}
	//Check to see if there is room to put into it. Is the size of the
	//request list equal to the size of the request list.

	//Add to the array, since the request is an actual request,
	//we need to malloc a new request.
	//copy all the values of the malloc'ed request into the given request.
	//Increment the size of the array.
}
int process_delRequest(Process* process, int request_num)
{
	//decrement the size and free the request.
	if(process->my_num_requests < 1 || process->my_num_requests < request_num)
	{
		return -1;
	}
	else
	{
    //
    free(process->my_requests_ptr[request_num]);
		process->my_num_requests--;
    process->my_requests_ptr[request_num] = 
         process->my_requests_ptr[process->my_num_requests];
		return 0;
	}
}
int process_getStartTime(Process* process){
	return process->start_clock;
}

void process_setLength(Process* process, int length){
	process->my_length = length;
}

void process_setStartTime(Process* process, int start_time)
{
  process->start_clock = start_time;
}